package 课上练习0422_scala
import scala.annotation.tailrec

/*Σn+Σn-1+.. +Σ1*/

object Test {
  def sum2(n: Int): Int = {
    var sum = 0
    for (i <- 1 to n) {
      sum += Range(1, i + 1).sum
    }
    sum
  }

  def sum1(n: Int): Int = {
    if (n == 1) n
    else Range(1, n + 1).sum + sum1(n - 1)
  }

  @tailrec
  def sum3(n: Int, sum: Int): Int = {
    if (n == 1) sum + n
    else sum3(n - 1, sum + Range(1, n + 1).sum)
  }


  def sum4(n: Int): Int = {
//    @tailrec
    def loop(n: Int, sum: Int): Int = {
      if (n == 1) sum + n
      else loop(n - 1, sum + Range(1, n + 1).sum)
    }

    loop(n, 0)
  }

  def sum5(n: Int): Int = {
    Range(1, n + 1).map(i => Range(1, i + 1).sum).sum
  }
//  1+..+n

  def main(args: Array[String]): Unit = {
    println(sum4(70000000))
//    println(sum5(7000))
  }
}